package com.fr.schedule.trigger;

import com.fr.base.StringUtils;
import com.fr.base.core.ComparatorUtils;
import com.fr.base.core.DateUtils;
import com.fr.base.core.json.JSONException;
import com.fr.base.core.json.JSONObject;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.schedule.dao.FieldMapper;
import com.fr.schedule.dao.ObjectMapper;
import com.fr.schedule.dao.ValueFieldMapper;
import com.fr.third.org.quartz.CronTrigger;
import com.fr.third.org.quartz.Trigger;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.TimeZone;
import oracle.sql.TIMESTAMP;

public class CalendarITrigger
  implements ITrigger
{
  public static final String TABLE_NAME = "fr_calendar_itrigger";
  public static final int IMMEDIATE = 1;
  public static final int ON = 2;
  public static final int EVERY_DAY = 1;
  public static final int WEEK_DAYS = 2;
  public static final int MONTH_DAYS = 3;
  public static final String ALL = "*";
  private long id = -1L;
  private long version;
  private String timeZoneID = TimeZone.getDefault().getID();
  private long startType = 1L;
  private Date startTime = null;
  private Date endTime = null;
  private long minutes = 0L;
  private long hours = 0L;
  private long dayType = 1L;
  private String weekDays = "*";
  private String monthDays = "*";
  private String months = "*";
  public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper("fr_calendar_itrigger", new FieldMapper[] { new FieldMapper("id", 4, "id"), new FieldMapper("timeZoneID", 12, "timezone_id"), new FieldMapper("startType", 4, "start_type"), new ValueFieldMapper("startTime", 93, "startTime")
  {
    public Object field2Value(Object paramObject)
    {
      return paramObject;
    }

    public Object value2Field(Object paramObject)
    {
      if (paramObject instanceof Timestamp)
        return new Date(((Timestamp)paramObject).getTime());
      if (paramObject instanceof TIMESTAMP)
        try
        {
          return new Date(((TIMESTAMP)paramObject).timestampValue().getTime());
        }
        catch (SQLException localSQLException)
        {
          localSQLException.printStackTrace();
        }
      return paramObject;
    }
  }
  , new ValueFieldMapper("endTime", 93, "endTime")
  {
    public Object field2Value(Object paramObject)
    {
      return paramObject;
    }

    public Object value2Field(Object paramObject)
    {
      if (paramObject instanceof Timestamp)
        return new Date(((Timestamp)paramObject).getTime());
      if (paramObject instanceof TIMESTAMP)
        try
        {
          return new Date(((TIMESTAMP)paramObject).timestampValue().getTime());
        }
        catch (SQLException localSQLException)
        {
          localSQLException.printStackTrace();
        }
      return paramObject;
    }
  }
  , new FieldMapper("minutes", 4, "minutes"), new FieldMapper("hours", 4, "hours"), new FieldMapper("dayType", 4, "day_type"), new FieldMapper("weekDays", 12, "week_days"), new FieldMapper("monthDays", 12, "month_days"), new FieldMapper("months", 12, "months") });

  public static CalendarITrigger analyzeJSON(JSONObject paramJSONObject)
  {
    CalendarITrigger localCalendarITrigger;
    try
    {
      String str3;
      localCalendarITrigger = new CalendarITrigger();
      long l1 = paramJSONObject.getLong("startType");
      localCalendarITrigger.setStartType(l1);
      if (l1 == 2L)
      {
        str1 = paramJSONObject.getString("startTime");
        try
        {
          Date localDate1 = DateUtils.DATETIMEFORMAT2.parse(str1);
          localCalendarITrigger.setStartTime(localDate1);
        }
        catch (ParseException localParseException1)
        {
          localParseException1.printStackTrace();
        }
      }
      String str1 = paramJSONObject.getString("endTime");
      if (StringUtils.isNotBlank(str1))
        try
        {
          Date localDate2 = DateUtils.DATETIMEFORMAT2.parse(str1);
          localCalendarITrigger.setEndTime(localDate2);
        }
        catch (ParseException localParseException2)
        {
          localParseException2.printStackTrace();
        }
      long l2 = paramJSONObject.getLong("dayType");
      localCalendarITrigger.setDayType(l2);
      long l3 = paramJSONObject.getLong("minutes");
      localCalendarITrigger.setMinutes(l3);
      long l4 = paramJSONObject.getLong("hours");
      localCalendarITrigger.setHours(l4);
      String str2 = paramJSONObject.getString("months");
      localCalendarITrigger.setMonths(str2);
      if (l2 == 2L)
      {
        str3 = paramJSONObject.getString("weekDays");
        localCalendarITrigger.setWeekDays(str3);
      }
      else if (l2 == 3L)
      {
        str3 = paramJSONObject.getString("monthDays");
        localCalendarITrigger.setMonthDays(str3);
      }
      if (paramJSONObject.has("id"))
        localCalendarITrigger.setId(paramJSONObject.getLong("id"));
      return localCalendarITrigger;
    }
    catch (JSONException localJSONException)
    {
      localJSONException.printStackTrace();
    }
    return null;
  }

  public JSONObject createJSONConfig()
  {
    JSONObject localJSONObject = new JSONObject();
    try
    {
      Date localDate;
      String str;
      localJSONObject.put("id", getId());
      localJSONObject.put("timeZoneID", getTimeZoneID());
      localJSONObject.put("startType", getStartType());
      if (2L == getStartType())
      {
        localDate = getStartTime();
        str = DateUtils.DATETIMEFORMAT2.format(localDate);
        localJSONObject.put("startTime", str);
      }
      localJSONObject.put("recurrenceType", "calendar");
      if (getEndTime() != null)
      {
        localDate = getEndTime();
        str = DateUtils.DATETIMEFORMAT2.format(localDate);
        localJSONObject.put("endTime", str);
      }
      long l1 = getMinutes();
      localJSONObject.put("minutes", l1);
      long l2 = getHours();
      localJSONObject.put("hours", l2);
      long l3 = getDayType();
      localJSONObject.put("dayType", l3);
      if (l3 == 2L)
        localJSONObject.put("weekDays", getWeekDays());
      else if (l3 == 3L)
        localJSONObject.put("monthDays", getMonthDays());
      localJSONObject.put("months", getMonths());
    }
    catch (JSONException localJSONException)
    {
      localJSONException.printStackTrace();
    }
    return localJSONObject;
  }

  public Trigger createTrigger()
  {
    CronTrigger localCronTrigger = new CronTrigger("trigger_" + getId() + "_" + getITriggerIdPrefix(), "ReportJobs");
    if (getStartType() == 1L)
      break label95:
    if ((getStartType() == 2L) && (getStartTime() != null))
      localCronTrigger.setStartTime(getStartTime());
    else
      throw new IllegalStateException("The statemenet is illegal.");
    if (getEndTime() != null)
      label95: localCronTrigger.setEndTime(getEndTime());
    StringBuffer localStringBuffer = new StringBuffer();
    localStringBuffer.append('0').append(' ');
    localStringBuffer.append(getMinutes()).append(' ');
    localStringBuffer.append(getHours()).append(' ');
    if (getDayType() == 1L)
    {
      localStringBuffer.append('*').append(' ');
      localStringBuffer.append(getMonths()).append(' ');
      localStringBuffer.append('?');
    }
    else if (getDayType() == 2L)
    {
      localStringBuffer.append('?').append(' ');
      localStringBuffer.append(getMonths()).append(' ');
      localStringBuffer.append(getWeekDays());
    }
    else if (getDayType() == 3L)
    {
      localStringBuffer.append(getMonthDays()).append(' ');
      localStringBuffer.append(getMonths()).append(' ');
      localStringBuffer.append('?');
    }
    try
    {
      localCronTrigger.setCronExpression(localStringBuffer.toString());
    }
    catch (ParseException localParseException)
    {
      localParseException.printStackTrace();
    }
    localCronTrigger.setTimeZone(TimeZone.getTimeZone(this.timeZoneID));
    return localCronTrigger;
  }

  public int getITriggerIdPrefix()
  {
    return 3;
  }

  public void setId(long paramLong)
  {
    this.id = paramLong;
  }

  public long getId()
  {
    return this.id;
  }

  public void setVersion(long paramLong)
  {
    this.version = paramLong;
  }

  public long getVersion()
  {
    return this.version;
  }

  public void setTimeZoneID(String paramString)
  {
    this.timeZoneID = paramString;
  }

  public String getTimeZoneID()
  {
    return this.timeZoneID;
  }

  public void setStartType(long paramLong)
  {
    this.startType = paramLong;
  }

  public long getStartType()
  {
    return this.startType;
  }

  public void setStartTime(Date paramDate)
  {
    this.startTime = paramDate;
  }

  public Date getStartTime()
  {
    return this.startTime;
  }

  public void setEndTime(Date paramDate)
  {
    this.endTime = paramDate;
  }

  public Date getEndTime()
  {
    return this.endTime;
  }

  public void setMinutes(long paramLong)
  {
    this.minutes = paramLong;
  }

  public long getMinutes()
  {
    return this.minutes;
  }

  public void setHours(long paramLong)
  {
    this.hours = paramLong;
  }

  public long getHours()
  {
    return this.hours;
  }

  public void setDayType(long paramLong)
  {
    this.dayType = paramLong;
  }

  public long getDayType()
  {
    return this.dayType;
  }

  public void setWeekDays(String paramString)
  {
    this.weekDays = paramString;
  }

  public String getWeekDays()
  {
    return this.weekDays;
  }

  public void setMonthDays(String paramString)
  {
    this.monthDays = paramString;
  }

  public String getMonthDays()
  {
    return this.monthDays;
  }

  public void setMonths(String paramString)
  {
    this.months = paramString;
  }

  public String getMonths()
  {
    return this.months;
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
  }

  public boolean equals(Object paramObject)
  {
    if (!(paramObject instanceof CalendarITrigger))
      return false;
    CalendarITrigger localCalendarITrigger = (CalendarITrigger)paramObject;
    return ((getId() == localCalendarITrigger.getId()) && (getVersion() == localCalendarITrigger.getVersion()) && (getStartType() == localCalendarITrigger.getStartType()) && (ComparatorUtils.equals(getStartTime(), localCalendarITrigger.getStartTime())) && (ComparatorUtils.equals(getEndTime(), localCalendarITrigger.getEndTime())) && (getMinutes() == localCalendarITrigger.getMinutes()) && (getHours() == localCalendarITrigger.getHours()) && (getDayType() == localCalendarITrigger.getDayType()) && (ComparatorUtils.equals(getWeekDays(), localCalendarITrigger.getWeekDays())) && (ComparatorUtils.equals(getMonthDays(), localCalendarITrigger.getMonthDays())) && (ComparatorUtils.equals(getMonths(), localCalendarITrigger.getMonths())));
  }

  public Object clone()
    throws CloneNotSupportedException
  {
    CalendarITrigger localCalendarITrigger = (CalendarITrigger)super.clone();
    return localCalendarITrigger;
  }
}